package com.ymt.reliable.message.service;

import com.ymt.AppStart;
import com.ymt.reliable.message.dao.TransactionMessageMapper;
import com.ymt.reliable.message.domain.TransactionMessage;
import com.ymt.reliable.message.domain.TransactionMessageFactory;
import com.ymt.reliable.message.enums.MessageStatus;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppStart.class)
public class TransactionMessageMapperTest3 {
    @Autowired
    private TransactionMessageMapper transactionMessageMapper;
    @Autowired
    private TransactionMessageIdService transactionMessageIdService;

    private String messageId;

    @Before
    public void setUp() throws Exception {
        TransactionMessage message = TransactionMessageFactory.createTransactionMessage("test", "test", "test");
        message.setMessageId(transactionMessageIdService.generateId());
        message.setStatus(MessageStatus.SENDING.getFlag());
        transactionMessageMapper.insert(message);
        messageId = message.getMessageId();
    }

    @Test
    public void update1() throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("timeout", 30L);
        List<TransactionMessage> messages = transactionMessageMapper.queryRetryList(params);
        Assert.assertTrue(messages == null || messages.size() == 0);
    }

    @Test
    public void update2() throws Exception {
        Thread.sleep(40000L);

        Map<String, Object> params = new HashMap<>();
        params.put("timeout", 30L);
        List<TransactionMessage> messages = transactionMessageMapper.queryRetryList(params);
        Assert.assertTrue(messages != null && messages.size() > 0);
    }
}
